package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JSONResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    // 处理查询所有部门请求
    @RequestMapping("/list")
    //@RequiredPermission(name = "权限列表")
    @RequiresPermissions(value = {"permission:list", "权限列表"}, logical = Logical.OR)
    public String list(Model model, QueryObject qo) {
        PageInfo<Permission> pageInfo = permissionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "permission/list";  // 找视图 /WEB-INF/views/permission/list.jsp
    }

    // 处理删除所有部门请求
    @RequestMapping("/delete")
    @RequiresPermissions(value = {"permission:delete", "权限删除"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        System.out.println("delete");
        System.out.println("id" + id);
        try {
            if (id != null) {
                // 传id值就删除对应的部门
                permissionService.delete(id);
            }
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "操作失败, 请联系管理员");
        }

    }

    // 处理去新增的请求 http://localhost/permission/input.do
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        // 传id 去修改
        if (id != null) {
            model.addAttribute("permission", permissionService.get(id));
            return "permission/input";
        }
        // 无id 去新增
        return "permission/input";  // 找视图 /WEB-INF/views/permission/input.jsp
    }

    // 处理新增或修改请求 http://localhost/permission/saveOrUpdate.do
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission) {
        if (permission.getId() != null) {
            permissionService.update(permission);
        } else {
            permissionService.save(permission);
        }
        return "redirect:/permission/list.do";
    }

    @RequestMapping("/reload")
    @RequiresPermissions(value = {"permission:reload", "权限重载"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult reload() {
        try {
            permissionService.reload();
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "亲, 加载失败了, 请联系管理员");
        }
    }

}


